本文同步更新於blog
情境:讓我們試著作一個摩斯電碼機,它會將一般句子轉成摩斯電碼的表示
<?php
namespace App\InterpreterPattern\MorseCode;
class Context
{
    /**
     * @var string
     */
    public $text;
    /**
     * @param string $text
     */
    public function __construct(string $text)
    {
        $this->text = $text;
    }
}
主要是承載要解譯的詞句,
會隨著解譯進度,改變其內容。
<?php
namespace App\InterpreterPattern\MorseCode\Contracts;
use App\InterpreterPattern\MorseCode\Context;
interface Expression
{
    /**
     * 找出要解析的字串執行,並回傳剩餘字串
     *
     * @param Context $context
     * @return Context
     */
    public function interpret(Context $context): Context;
    /**
     * 解析字串後,印在控制台
     *
     * @param string $message
     */
    public function execute(string $message);
}
這邊說明一下,所謂的摩斯電碼,
是利用滴答兩種不同長短訊號的排列組合,
來表達每一個字母符號。
例如:A的表示為 (.-)。
而在此處的範例中,
同個單字的字母會用空格 ( ) 分開,
不同單字的字母則會用斜槓 (/) 分開。
例如:Good Morning的表示會是 (--. --- --- -.. / -- --- .-. -. .. -. --.)。
字母間不區分大小寫。
按照上述規則,我想區分出兩種表達式 (Expression)。
解譯字母符號的為終端表達式 (Terminal Expression),
其他情況為非終端表達式 (NonTerminal Expression)。
想法是使用非終端表達式時,表示還有字需要解譯。
<?php
namespace App\InterpreterPattern\MorseCode;
use App\InterpreterPattern\MorseCode\Contracts\Expression;
use App\InterpreterPattern\MorseCode\Context;
class NonTerminalExpression implements Expression
{
    public function interpret(Context $context): Context
    {
        $head = ' ';
        $context->text = trim($context->text);
        $this->execute($head);
        return $context;
    }
    /**
     * @param string $message
     */
    public function execute(string $message)
    {
        echo ' / ';
    }
    /**
     * @param string $character
     * @return boolean
     */
    public function isSpace($character)
    {
        return $character == ' ';
    }
}
此處interpret()方法會將目前解譯到的詞句,去除前後空白。
execute()方法則會印出斜槓 (/)。
而isSpace()方法,會在待會的客戶端程式碼用到。
<?php
namespace App\InterpreterPattern\MorseCode;
use App\InterpreterPattern\MorseCode\Contracts\Expression;
use App\InterpreterPattern\MorseCode\Context;
use App\InterpreterPattern\MorseCode\Exceptions\UndefinedTextException;
class TerminalExpression implements Expression
{
    protected $mapping = [
        'a' => '.-',
        'b' => '-...',
        'c' => '-.-.',
        'd' => '-..',
        'e' => '.',
        'f' => '..-.',
        'g' => '--.',
        'h' => '....',
        'i' => '..',
        'j' => '.---',
        'k' => '-.-',
        'l' => '.-..',
        'm' => '--',
        'n' => '-.',
        'o' => '---',
        'p' => '.--.',
        'q' => '--.-',
        'r' => '.-.',
        's' => '...',
        't' => '-',
        'u' => '..-',
        'v' => '...-',
        'w' => '.--',
        'x' => '-..-',
        'y' => '-.--',
        'z' => '--..',
        '0' => '-----',
        '1' => '.----',
        '2' => '..---',
        '3' => '...--',
        '4' => '....-',
        '5' => '.....',
        '6' => '-....',
        '7' => '--...',
        '8' => '---..',
        '9' => '----.',
        '.' => '.-.-.-',
        ',' => '--..--',
        '?' => '..--..',
        '/' => '-..-.',
        "'" => '.----.',
        '!' => '-.-.--',
    ];
    public function interpret(Context $context): Context
    {
        $firstSpacePos = strpos($context->text, ' ');
        if ($firstSpacePos) {
            $head = substr($context->text, 0, $firstSpacePos);
            $context->text = substr($context->text, $firstSpacePos);
        } else {
            $head = $context->text;
            $context->text = '';
        }
        $this->execute($head);
        return $context;
    }
    /**
     * @param string $message
     */
    public function execute(string $message)
    {
        $characters = str_split($message);
        $lastKey = array_key_last($characters);
        foreach ($characters as $key => $character) {
            $this->encode($character);
            if ($key == $lastKey) {
                break;
            }
            $this->typeSpace();
        }
    }
    /**
     * @param string $character
     */
    private function encode(string $character)
    {
        $character = strtolower($character);
        if (!array_key_exists($character, $this->mapping)) {
            throw new UndefinedTextException();
        }
        echo $this->mapping[$character];
    }
    private function typeSpace()
    {
        echo ' ';
    }
}
此處interpret()方法會找出要解譯的單字,並截斷它。
execute()方法則會逐步印出單字中的每一個字母符號,彼此間以空格隔開。
<?php
namespace App\InterpreterPattern\MorseCode;
use App\InterpreterPattern\MorseCode\NonTerminalExpression;
use App\InterpreterPattern\MorseCode\TerminalExpression;
use App\InterpreterPattern\MorseCode\Context;
class Program
{
    /**
     * @var TerminalExpression
     */
    protected $terminalExpression;
    /**
     * @var NonTerminalExpression
     */
    protected $nonTerminalExpression;
    public function __construct()
    {
        $this->terminalExpression = new TerminalExpression();
        $this->nonTerminalExpression = new NonTerminalExpression();
    }
    /**
     * @param string $text
     */
    public function encode(string $text)
    {
        try {
            $context = new Context(trim($text));
            while (strlen($context->text) > 0) {
                $firstCharacter = substr($context->text, 0, 1);
                if ($this->nonTerminalExpression->isSpace($firstCharacter)) {
                    $context = $this->nonTerminalExpression->interpret($context);
                    continue;
                }
                $context = $this->terminalExpression->interpret($context);
            }
        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
最後讓我們來看客戶端程式碼怎麼跑吧!
以Hello World為例:
[單一職責原則]
語境類別 (Context):負責乘載要解譯的詞句。
非終端表達式 (NonTerminal Expression):負責連結解譯單字間的文法。
終端表達式 (Terminal Expression):負責解譯每一個字母符號。
[開放封閉原則]
增加要轉譯的字母符號時,僅需修改終端表達式 (Terminal Expression)。
[依賴反轉原則]
透過表達式 (Expression) 接口,
確保各個表達式都有interpret()方法與execute()方法。
最後附上類別圖:
(註:若不熟悉 UML 類別圖,可參考UML類別圖說明。)
現實中幾乎沒有機會使用到的設計模式,
範例想了很多天,希望這樣有傳達出這個模式的精神!
另外這個範例還沒有完成decode()方法,
也就是從摩斯電碼轉回一般句子。
之後有時間會試著實作看看。
ʕ •ᴥ•ʔ:目前心目中前三難的設計模式。